package com.hc.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.dao.IRepairPoolDAO;
import com.hc.dao.IRepairUserDAO;
import com.hc.dao.IStaffDAO;
import com.hc.entity.RRepairPool;
import com.hc.entity.RRepairUser;
import com.hc.entity.UUser;
import com.hc.exception.DataConstraintViolationException;
import com.hc.exception.FieldNotExistException;
import com.hc.service.IDataDictService;
import com.hc.service.IRepairPoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.locks.Condition;


@Service
public class RepairPoolServiceImpl implements IRepairPoolService {

    IRepairPoolDAO poolDAO;
    IDataDictService dataDictService;
    IStaffDAO staffDAO;
    IRepairUserDAO repairUserDAO;


    @Autowired
    public RepairPoolServiceImpl(IRepairPoolDAO poolDAO, IDataDictService dataDictService, IStaffDAO staffDAO, IRepairUserDAO repairUserDAO) {
        this.poolDAO = poolDAO;
        this.dataDictService = dataDictService;
        this.staffDAO = staffDAO;
        this.repairUserDAO = repairUserDAO;
    }

    @Override
    @Transactional
    public void registerRepair(RRepairPool repair) {
        repair.setRepairId(String.valueOf(System.currentTimeMillis()));
        repair.setRepairObjName("瞎写的");
        if (!dataDictService.checkIfKeyExist(repair)) {
            throw new DataConstraintViolationException("参数错误");
        }
        //本来应该校验社区是否存在的。。。
        poolDAO.addRepair(repair);
    }

    @Override
    @Transactional
    public int updateRepairPool(RRepairPool content) {
        if (!dataDictService.checkIfKeyExist(content)) {
            return 0;
        }
        return poolDAO.updateById(content);
    }

    @Override
    @Transactional
    public void dispatchTickets(String repairId, String staffId) {
        List<String> staffNames = staffDAO.getStaffNameById(staffId);
        if (staffNames == null || staffNames.size() == 0) {
            throw new FieldNotExistException("该员工已经删除");
        }
        RRepairPool order = poolDAO.getByRepairId(repairId);
        if (order == null) {
            throw new FieldNotExistException("该工单已删除");
        }
        RRepairUser rRepairUser = new RRepairUser();
        rRepairUser.setRuId(String.valueOf(System.currentTimeMillis()));
        rRepairUser.setRepairId(String.valueOf(System.currentTimeMillis()));
        rRepairUser.setContext(order.getContext());
        rRepairUser.setRepairId(order.getRepairId());
        rRepairUser.setCommunityId(order.getCommunityId());
        rRepairUser.setState("10005");
        rRepairUser.setStaffId(staffId);
        rRepairUser.setStaffName(staffNames.get(0));
        repairUserDAO.addRepairUser(rRepairUser);
    }

    @Override
    public PageInfo<RRepairPool> getRepairByConditions(int pageNum, int pageSize, RRepairPool condition) {
        Page<RRepairPool> page = PageHelper.startPage(pageNum, pageSize);
        PageInfo<RRepairPool> pageInfo = page.doSelectPageInfo(() -> poolDAO.getByConditions(condition));
        pageInfo.getList().forEach(ent -> {
            dataDictService.fillValByDictIfExist(ent);
        });
        page.close();
        return pageInfo;
    }

}
